let isShowIng = false;
let showTimer, closeTimer;


function show() {

	if (isShowIng) {
		return;
	}
	if (showTimer) {
		clearTimeout(showTimer);
	}

	showTimer = setTimeout(() => {
		uni.showLoading({
			title: '加载中...',
			mask: true,
			complete: () => {
				isShowIng = true;
			}
		})
	}, 500);

	if (closeTimer) {
		clearTimeout(closeTimer)
	}
	closeTimer = setTimeout(() => {
		if (isShowIng) {
			uni.hideLoading();
		}
	}, 10 * 1000);

}

function hide() {
	if (isShowIng) {
		uni.hideLoading();
	}

	if (showTimer) {
		clearTimeout(showTimer);
	}
	isShowIng = false;
}

export const setStorage = (key,value)=>{
	//new Promise((resolve,reject) =>{
		uni.setStorageSync(key, value);
	//})
}

export const getStorage = (key)=>{
	return uni.getStorageSync(key) || '';
}

export const toast = (title,icon='none',duration=2000)=>{
	uni.showToast({
		title,
		icon,
		duration
	});
}

const zeroFill = (num)=> {
	return num < 10 ? '0' + num : num;
}

export const dateFormat = (fmt, time)=> {
	let date = new Date();
	if (time) {
		date.setTime(time);
	}

	let o = {
		"Y": date.getFullYear(),
		"m": zeroFill(date.getMonth() + 1),
		"d": zeroFill(date.getDate()),
		"H": zeroFill(date.getHours()),
		"i": zeroFill(date.getMinutes()),
		"s": zeroFill(date.getSeconds()),
	};
	for (let k in o) {
		if (fmt.indexOf(k) > -1) {
			fmt = fmt.replace(new RegExp("(" + k + ")"), o[k]);
		}
	}
	return fmt;
}

export const checkMobile = phone =>{
    const myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
    if (!myreg.test(phone)) {
        return false;
    } else {
        return true;
    }
}


export default {
	show: show,
	hide: hide,
}
